/*
 * 项目名称:    wms-server-jeecg
 * 创建人员:    hansen
 * 创建时间:    2024-01-26
 */
package org.jeecg.modules.dock.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.druid.sql.visitor.functions.Char;
import com.alibaba.fastjson.JSON;
import io.swagger.util.Json;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.dock.client.VMSRequestClient;
import org.jeecg.modules.dock.config.JsonUtils;
import org.jeecg.modules.dock.constant.SystemConstants;
import org.jeecg.modules.dock.entity.DockDataSendRecord;
import org.jeecg.modules.dock.entity.mes.response.MesBaseResponseVO;
import org.jeecg.modules.dock.entity.nest.NestUpdateStatusVO;
import org.jeecg.modules.dock.entity.vms.request.VMSRequestVO;
import org.jeecg.modules.dock.entity.vms.response.VMSResponseVO;
import org.jeecg.modules.dock.enums.DockDataStatus;
import org.jeecg.modules.dock.service.IDockDataSendRecordService;
import org.jeecg.modules.dock.service.VmsDockingRepeaterService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * VMS实现类
 *
 * @author hansen
 * @version 1.0.0
 * @date 2024-01-26 09:49:18
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class VmsDockingRepeaterServiceImpl implements VmsDockingRepeaterService {

    private final VMSRequestClient vmsRequestClient;

    private final IDockDataSendRecordService dockDataSendRecordService;

    @Override
    public VMSResponseVO signalTrigger(VMSRequestVO baseId) {
        DockDataSendRecord record = getRecord(JsonUtils.toJson(baseId));
        VMSResponseVO vmsResponseVO = null;
        try {
            String json = JsonUtils.toJson(baseId);
            vmsResponseVO = vmsRequestClient.signalTrigger(json);
            record.setStatus(DockDataStatus.SUCCESS);
            record.setResultCode(CommonConstant.ANNOUNCEMENT_SEND_STATUS_0);
            record.setResultData(vmsResponseVO.toString());
        } catch (Exception e) {
            record.setStatus(DockDataStatus.FAILURE);
            record.setResultData(e.toString());
            record.setResultCode(CommonConstant.ANNOUNCEMENT_SEND_STATUS_1);
            log.error("更新VMS状态异常信息: ", e);
            return VMSResponseVO.failure(e.getMessage());
        } finally {
            dockDataSendRecordService.save(record);
        }
        return vmsResponseVO;
    }

    private DockDataSendRecord getRecord(String data) {
        DockDataSendRecord sendRecord = new DockDataSendRecord();
        sendRecord.setSource(SystemConstants.WMS);
        sendRecord.setTarget(SystemConstants.VMS);
        sendRecord.setUuid(IdUtil.fastSimpleUUID());
        sendRecord.setSendTime(new Date());
        sendRecord.setData(data);
        return sendRecord;
    }
}
